<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table{
            border: 1px solid;
        }
         td{
            text-align: center;
        }
    </style>
</head>
<body>
    <div id="app">
        <table>
            <thead>
            <tr>
                <th><input type="checkbox" v-model="checkAll"></th>
                <th>编号</th>
                <th>名称</th>
                <th>价格</th>
                <th>数量</th>
                <th>小计</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
                <tr v-for="item in goods" :key="item.id">
                    <td><input type="checkbox" v-model="item.isCheck"></td>
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.price}}</td>
                    <td>
                        <BUTTON @click="item.num--">-</BUTTON>
                        <span>{{item.num}}</span>
                        <button @click="item.num++">+</button>
                    </td>
                    <td>
                        {{item.priceTotal * item.num}}
                    </td>
                    <td>
                       <button @click="del(item.id)">{{item.del}}</button> 
                    </td>
                  
                </tr>    
                    <tr>
                    <td>商品数量：{{totalNum}} 个 </td>  
                    <td>合计: ￥ {{totalPrice}}元</td> 
                </tr>
            </tbody>
          
            
        
            
        </table>
        
    </div>
</body>
<script src="./vue.js"></script>
<script>
       let vm = new Vue({
        el: "#app",
        data:{
           goods:[
            {isCheck: true,id:1,name:'铅笔',price:1.5,num:1, priceTotal: 1.5,del:'删除'},
            {isCheck:false,id:2,name:'钢笔',price:45,num:1, priceTotal: 45,del:'删除'},
            {isCheck:true,id:3,name:'签字笔',price:2.5,num:1, priceTotal: 2.5,del:'删除'},
            {isCheck:true,id:4,name:'毛笔',price:35,num:1, priceTotal: 35,del:'删除'},
           ]
        },
        methods:{
           isCheck(){
            this.isCheck =!this.isCheck
           },
           del(id){
            console.log('删除',id)
            this.goods =  this.goods.filter(item =>item.id !== id)
           }

        },
        computed:{
            totalNum(item){
                let sum = 0;
                this.goods.forEach(item=>{
                if(item.isCheck){
                    sum +=item.num;
                }
            })
              return sum
            },
            checkAll:{
                        
                get(){
                    return this.goods.every(item => item.isCheck)
                },
                set(val){
                    console.log(val) //true ,false
                    return this.goods.forEach(item =>item.isCheck = val)
                }
            
            },
            totalPrice(item){
                let sum = 0;
                this.goods.forEach(item=>{
                if(item.isCheck){
                    sum +=item.price *item.num;
                }
            })
              return sum
            }


        }
        
    })
</script>
</html>